package com.coollang.smartbra.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.coollang.smartbra.R;
import com.coollang.smartbra.model.SportRecordModel;
import com.coollang.smartbra.utils.DisplayUtils;

import java.util.List;

/**
 * 运动统计页面天页面
 */
public class DaySportCharAdapter extends BaseAdapter {
    private final Context mContext;
    private int currentPoition = -1;
    //可以绘制的最大高度
    int maxHeight;
    //获取float类型中最大的值
    float maxMileage;

    private List<SportRecordModel.DataBean> mDataBeanList;

    public DaySportCharAdapter(Context context, int height) {
        this.mContext = context;
        maxHeight = height - DisplayUtils.dip2px(context, 418);
    }

    @Override
    public int getCount() {
        if (null != mDataBeanList) {
            return mDataBeanList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return mDataBeanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_sport_statistics_day, parent, false);
            holder = new Holder();
            holder.iv_chart = (ImageView) convertView.findViewById(R.id.iv_chart);
            holder.iv_point = (ImageView) convertView.findViewById(R.id.iv_point);
            holder.tv_date = (TextView) convertView.findViewById(R.id.tv_date);
            convertView.setTag(holder);

           // AutoUtils.autoSize(convertView);
        } else {
            holder = (Holder) convertView.getTag();
        }


        SportRecordModel.DataBean dataBean = mDataBeanList.get(position);

        // 日期
        holder.tv_date.setText(getDate(dataBean.getDate()));

        if (position == getCount() - 1) {
            holder.tv_date.setText("今日");
        }

        //次数  时间  步数
        setIvChartHeightFloat(holder, dataBean.getCount());

        if (currentPoition == position) {
            holder.iv_chart.setBackgroundColor(mContext.getResources().getColor(R.color.progrss_bg_80));
            holder.iv_point.setVisibility(View.VISIBLE);
            holder.tv_date.setTextColor(mContext.getResources().getColor(R.color.progrss_bg));
        } else {
            holder.iv_chart.setBackgroundColor(mContext.getResources().getColor(R.color.progrss_bg_12));
            holder.iv_point.setVisibility(View.INVISIBLE);
            holder.tv_date.setTextColor(mContext.getResources().getColor(R.color.black_666_50));
        }

        return convertView;
    }

    private static class Holder {
        public ImageView iv_chart;
        public ImageView iv_point;
        public TextView tv_date;
    }

    //*************************************工具类**********************************************//

    /**
     * 设置iv_chart的显示
     */
    private void setIvChartHeightFloat(Holder holder, float times) {
        if (times == 0 || maxMileage == 0) {
            holder.iv_chart.setLayoutParams(getLayoutParams((int) (maxHeight * 0.01)));
        } else if (times >= maxMileage) {
            holder.iv_chart.setLayoutParams(getLayoutParams(maxHeight));
        } else {
            if ((int) (maxHeight * times / maxMileage) < (int) (maxHeight * 0.01)) {
                holder.iv_chart.setLayoutParams(getLayoutParams((int) (maxHeight * 0.01)));
            } else {
                holder.iv_chart.setLayoutParams(getLayoutParams((int) (maxHeight * times / maxMileage)));
            }
        }
    }

    /**
     * 获取布局属性
     */
    @NonNull
    private LinearLayout.LayoutParams getLayoutParams(int maxHeight) {
        return new LinearLayout.LayoutParams(DisplayUtils.dip2px(mContext, 30), maxHeight);
    }

    /**
     * 条目选中更新显示状态
     */
    public void notifOnItemClick(int position) {
        this.currentPoition = position;
        notifyDataSetChanged();
    }

    /**
     * 周或者日选中更新显示状态
     */
    public void notifWeekOrDay(List<SportRecordModel.DataBean> mlist) {
        mDataBeanList = mlist;
        getMaxMileage();
        currentPoition = mDataBeanList.size() - 1;
        notifyDataSetChanged();
    }


    private void getMaxMileage() {
        int max = 0;
        for (SportRecordModel.DataBean dataBean : mDataBeanList) {
            int count = dataBean.getCount();
            if (count > max) {
                max = count;
            }
        }
        maxMileage = max;
    }

    public static String getDate(String date) {
        String[] split = date.split("-");
        int month = Integer.parseInt(split[split.length - 2]);
        int day = Integer.parseInt(split[split.length - 1]);

        return month + "/" + day;
    }
}
